NSButton控件用来创建功能按钮,和UIButton相比,其样式要丰富许多。NSButton继承自NSControl,其使用setTarget与setAction来添加触发方法,如下:
1 2 3 4
| NSButton * btn = [[NSButton alloc]initWithFrame:CGRectMake(50, 300, 90, 25)]; [btn setTarget:self]; [btn setAction:@selector(click)]; [self.view addSubview:btn];
|
NSButton类中常用属性和方法解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| @property (copy) NSString *title;
@property (copy) NSString *alternateTitle;
@property (nullable, strong) NSImage *image;
@property (nullable, strong) NSImage *alternateImage;
@property NSCellImagePosition imagePosition;
@property NSImageScaling imageScaling NS_AVAILABLE_MAC(10_5);
@property BOOL imageHugsTitle;
@property NSInteger state;
@property (getter=isBordered) BOOL bordered;
@property (getter=isTransparent) BOOL transparent;
@property (copy) NSString *keyEquivalent;
@property (copy) NSAttributedString *attributedTitle; @property (copy) NSAttributedString *attributedAlternateTitle;
@property NSBezelStyle bezelStyle;
@property BOOL showsBorderOnlyWhileMouseInside;
@property (nullable, strong) NSSound *sound;
|
下面是一些便捷创建按钮的方法:
1 2 3 4 5 6 7 8 9 10
| + (instancetype)buttonWithTitle:(NSString *)title image:(NSImage *)image target:(nullable id)target action:(nullable SEL)action;
+ (instancetype)buttonWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
+ (instancetype)buttonWithImage:(NSImage *)image target:(nullable id)target action:(nullable SEL)action;
+ (instancetype)checkboxWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
+ (instancetype)radioButtonWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
|